//#version 400

/*

STOP!
This code from NGSA 4.0.Do not modify and distrybude this code without permssion.
Check out LICENSE.txt before you have will modify this code.

*/

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//external parameters, do not modify                                                                                 //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

float ch
<
	string UIName="Chroma - Power";
	string UIWidget="Spinner";
	float UIMin=0.0;
	float UIMax=1.0;
> = {0.0};

//keyboard controlled temporary variables (in some versions exists in the config file). Press and hold key 1,2,3...8 together with PageUp or PageDown to modify. By default all set to 1.0
float4	tempF1; //0,1,2,3
float4	tempF2; //5,6,7,8
float4	tempF3; //9,0
//x=generic timer in range 0..1, period of 16777216 ms (4.6 hours), w=frame time elapsed (in seconds)
float4	Timer;
//x=Width, y=1/Width, z=ScreenScaleY, w=1/ScreenScaleY
float4	ScreenSize;
//adaptation delta time for focusing
float FadeFactor;
//transposed transform matrix view*projection and inverse of it
float4	MatrixVP[4];
float4	MatrixInverseVP[4];
//textures
texture2D texColor;
texture2D texNoise;
texture2D texDepth;
texture2D texFocus;

sampler2D SamplerColor = sampler_state
{
	Texture   = <texColor>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;//NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerNoise = sampler_state
{
	Texture   = <texNoise>;
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = NONE;//NONE;
	AddressU  = Wrap;
	AddressV  = Wrap;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerDepth = sampler_state
{
	Texture = <texDepth>;
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = NONE;
	AddressU = Clamp;
	AddressV = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerFocus = sampler_state
{
	Texture = <texFocus>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;
	AddressU = Clamp;
	AddressV = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

struct VS_OUTPUT_POST {
	float4 vpos  : POSITION;
	float2 txcoord : TEXCOORD0;
};

struct VS_INPUT_POST {
	float3 pos  : POSITION;
	float2 txcoord : TEXCOORD0;
};

float linearizeDepth(float nonlinearDepth)
{
	float2 dofProj=float2(0.15, 3000);
	float2 dofDist=float2(0.0, 0.15);
	float4 depth=nonlinearDepth;
	
	depth.y=-dofProj.x + dofProj.y;
	depth.y=1.0/depth.y;
	depth.z=depth.y * dofProj.y;
	depth.z=depth.z * -dofProj.x;
	depth.x=dofProj.y * -depth.y + depth.x;
	depth.x=1.0/depth.x;
	depth.y=depth.z * depth.x;
	depth.x=depth.z * depth.x - dofDist.y;
	depth.x+=dofDist.x * -0.5;
	depth.x=max(depth.x, 0.0);
	return depth.x;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
{
	VS_OUTPUT_POST OUT;

	float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);

	OUT.vpos=pos;
	OUT.txcoord.xy=IN.txcoord.xy;

	return OUT;
}

float xy=3.0;float ZIMMER=1.0;float r=0.5;float4 l(float2 tex){float3 mc=pow(smoothstep(1,ZIMMER,distance(tex,float2(r,r)))+float3(1,.995,1),ch);return float4(float3(tex2D(SamplerColor,float2((2.0*tex-ZIMMER)*mc.r)*r+r).r,tex2D(SamplerColor,float2((2.0*tex-ZIMMER)*mc.g)*r+r).g,tex2D(SamplerColor,float2((2.0*tex-ZIMMER)*mc.b)*r+r).b)*(ZIMMER-smoothstep(1,ZIMMER,distance(tex,float2(r,r)))),ZIMMER);}

float4 PS_PostProcess(VS_OUTPUT_POST i):COLOR{

	#define FXAA_SUBPIX_SHIFT (1.0/8.0)
	#define FXAA_REDUCE_MIN   (1.0/32.0)
	#define FXAA_REDUCE_MUL   (1.0/16.0)
	#define FXAA_SPAN_MAX     8.0

	half2 rcpFrame = half2(1/ScreenSize.x, 1/(ScreenSize.x/ScreenSize.z));
	half4 posPos;posPos.xy = i.txcoord.xy;posPos.zw = posPos.xy - (rcpFrame.xy * (0.5 + FXAA_SUBPIX_SHIFT));
	half3 rgbNW = tex2D(SamplerColor, posPos.zw ).xyz;half3 rgbNE = tex2D(SamplerColor, posPos.zw + half2(rcpFrame.x, 0.0) ).xyz;half3 rgbSW = tex2D(SamplerColor, posPos.zw + half2(0.0, rcpFrame.y) ).xyz;half3 rgbSE = tex2D(SamplerColor, posPos.zw +rcpFrame.xy ).xyz;half3 rgbM  = tex2D(SamplerColor, posPos.xy).xyz;
       	half3 luma = half3(0.299, 0.587, 0.114);half lumaNW = dot(rgbNW, luma);half lumaNE = dot(rgbNE, luma);half lumaSW = dot(rgbSW, luma);half lumaSE = dot(rgbSE, luma);half lumaM = dot(rgbM, luma);
       	half lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));half lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
        half2 dir; 
        half lumaNWNE = lumaNW + lumaNE;half lumaSWSE = lumaSW + lumaSE;
        dir.x = -((lumaNWNE) - (lumaSWSE));dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));
       	half dirReduce = max( (lumaSWSE + lumaNWNE) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);half rcpDirMin = ZIMMER/(min(abs(dir.x), abs(dir.y)) + dirReduce);dir = min(half2( FXAA_SPAN_MAX,  FXAA_SPAN_MAX), max(half2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * rcpFrame.xy;
        half3 rgbA = (ZIMMER/2.0) * (tex2D(SamplerColor, posPos.xy + dir * (ZIMMER/3.0 - 0.5) ).xyz + tex2D(SamplerColor, posPos.xy + dir * (2.0/3.0 - 0.5) ).xyz);half3 rgbB = rgbA * (ZIMMER/2.0) + (ZIMMER/4.0) * (tex2D(SamplerColor, posPos.xy + dir * (0.0/3.0 - 0.5) ).xyz + tex2D(SamplerColor, posPos.xy + dir * (3.0/3.0 - 0.5) ).xyz);half lumaB = dot(rgbB, luma);
     	if((lumaB < lumaMin) || (lumaB > lumaMax))
 		return half4(rgbA, 1.0);
       	return float4(rgbB, 1.0);
}

float4 PS_PostProcess_Sharp_Noise(VS_OUTPUT_POST f):COLOR{float4 res=0;float4 tex=0.;tex.xy=f.txcoord.xy;float3 ori=tex2D(SamplerColor,tex).rgb;float2 p;p.x=1./ScreenSize.x;p.y=p.x/ScreenSize.z;
float3 blur_ori=tex2D(SamplerColor,tex+float2(.5*p.x,-p.y*.3)).rgb;blur_ori+=tex2D(SamplerColor,tex+float2(.3*-p.x,.5*-p.y)).rgb; blur_ori+=tex2D(SamplerColor,tex+float2(.3*p.x,0.5*p.y)).rgb;blur_ori+=tex2D(SamplerColor,tex+float2(.5*-p.x,p.y*.3)).rgb; 
blur_ori/=4.;float3 sharp=ori-blur_ori;float sharp_luma=dot(sharp,.3);sharp_luma=clamp(sharp_luma,-.5,.5);float4 done=tex2D(SamplerColor, tex)+sharp_luma;float origgray=dot(done.xyz, 0.333);done.w=1.;
return done;
}

float4 PS_Chroma(VS_OUTPUT_POST IN):COLOR{
float2 d=IN.txcoord.xy;float4 r=l(d.xy);r.a=1;
return r;
}

technique PostProcess //FXAA
{
   pass P0
   {
	VertexShader = compile vs_3_0 VS_PostProcess();
	PixelShader  = compile ps_3_0 PS_PostProcess_Sharp_Noise();
   }
}

technique PostProcess2 //FXAA
{
   pass P0
   {
	VertexShader = compile vs_3_0 VS_PostProcess();
	PixelShader  = compile ps_3_0 PS_PostProcess();
   }
}

technique PostProcess3 //FXAA
{
   pass P0
   {
	VertexShader = compile vs_3_0 VS_PostProcess();
	PixelShader  = compile ps_3_0 PS_PostProcess_Sharp_Noise();
   }
}

technique PostProcess4
{
  pass P0
  {
	VertexShader = compile vs_3_0 VS_PostProcess();
	PixelShader  = compile ps_3_0 PS_Chroma();
   }
}

